package com.dianrui.operation.activity;


import android.view.View;
import android.widget.TextView;

import com.dianrui.operation.Constant;
import com.dianrui.operation.R;
import com.dianrui.operation.okhttp.XHttpRequest;
import com.dianrui.operation.util.SharedUtil;
import com.dianrui.operation.util.StringUtils;
import com.dianrui.operation.util.ToastUtil;
import com.dianrui.operation.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;


public class BoxStatusInfoActivity extends BaseActivity {

    private String vehicleId;
    private String number;


    @BindView(R.id.box_number)
    TextView boxNumber;
    @BindView(R.id.car_number)
    TextView carNumber;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.voltage)
    TextView voltage;
    @BindView(R.id.acc)
    TextView acc;
    @BindView(R.id.fortify)
    TextView fortify;
    @BindView(R.id.motor_lock)
    TextView motorLock;
    @BindView(R.id.battery_lock)
    TextView batteryLock;
    @BindView(R.id.low_voltage)
    TextView lowVoltage;
    @BindView(R.id.battery)
    TextView battery;
    @BindView(R.id.journey)
    TextView journey;


    @Override
    public int getContentViewId() {
        return R.layout.box_status_info;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            vehicleId = getIntent().getStringExtra("vehicle_id");
            number = getIntent().getStringExtra("number");
        }
        title.setText("盒子状态");
        getBoxStatus();

    }

    private void getBoxStatus() {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("number", number);
        StringBuilder boxStatusBulider=new StringBuilder();
        boxStatusBulider.append(SharedUtil.getUrl()).append(Url.BOX_STATUS);
        XHttpRequest.getInstance().postRequests(boxStatusBulider.toString(), jsonObject.toString(), new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                carNumber.setText(jsonObject.optJSONObject("data").optString("number"));
                                String showVoltage = jsonObject.optJSONObject("data").optString("voltage") + "伏特";
                                voltage.setText(showVoltage);
                                if ("1".equals(jsonObject.optJSONObject("data").optString("acc"))) {
                                    acc.setText("开启");
                                } else {
                                    acc.setText("关闭");
                                }
                                if ("1".equals(jsonObject.optJSONObject("data").optString("fortify"))) {
                                    fortify.setText("设防");
                                } else {
                                    fortify.setText("撤防");
                                }
                                if ("1".equals(jsonObject.optJSONObject("data").optString("motor_lock"))) {
                                    motorLock.setText("锁定");
                                } else {
                                    motorLock.setText("开启");
                                }
                                if ("1".equals(jsonObject.optJSONObject("data").optString("batteryLock"))) {
                                    batteryLock.setText("开启");
                                } else {
                                    batteryLock.setText("关闭");
                                }
                                boxNumber.setText(jsonObject.optJSONObject("data").optString("device"));
                                battery.setText(jsonObject.optJSONObject("data").optString("battery"));
                                journey.setText(jsonObject.optJSONObject("data").optString("journey"));
                            } else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1002".equals(jsonObject.optString("status"))) {
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @Override
    public void initData() {
    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;

        }
    }




}
